package com.suse.common.base.model.entity;

/**
 * <p>
 * 分页
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-29 16:03
 */
public class Page {

    private int current = 1;

    private int limit = 10;

    private long total;

    private String path;

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        if (current >= 1) {
            this.current = current;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int size) {
        if (size >= 1 && size <= 100) {
            this.limit = size;
        }
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    /**
     * 获取起始行
     * @return 起始行
     */
    public int getOffset() {
        return (current - 1) * limit;
    }

    /**
     * 获取总页数
     * @return 总页数
     */
    public long getPages() {
        if (total % limit == 0) {
            return total / limit;
        }else {
            return total / limit + 1;
        }
    }

    /**
     * 获取起始页码
     * @return 起始页码
     */
    public int getFrom() {
        int from = current - 2;
        return Math.max(from, 1);
    }

    /**
     * 获取结束页码
     * @return 结束页码
     */
    public long getTo() {
        int to = current + 2;
        return Math.min(to, getPages());
    }

}
